package com.supinfo.sun.sungamers.games;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;

import com.supinfo.sun.sungamercities.dao.impl.CitiesFile;
import com.supinfo.sun.sungamers.enums.Level;
import com.supinfo.sun.sungamers.exceptions.UserAttemptException;
import com.supinfo.sun.sungamers.threads.DurationGameRunnable;

/**
 * @author Liu, Bing(Locke)
 * @since 2011-04-05
 * @version 0.3
 */
public class CapitalFinderGame extends FinderGame {

	List<String> usersAttempts;
	String[] cities;

	public CapitalFinderGame(Level level) {
		this.level = level;
	}

	public void initGame() {
		super.initGame();
		// try {
		// Socket client = new Socket("localhost", 33000);
		// InputStream in = client.getInputStream();
		// byte[] buffer = new byte[64];
		// String rule = null;
		//
		// while (in.read(buffer) != -1) {
		// rule = new String(buffer);
		// System.out.println(rule);
		// }
		// } catch (UnknownHostException e) {
		// System.out.println("CapitalFinderGame UnknownHostException"
		// + e.getMessage());
		// } catch (IOException e) {
		// System.out
		// .println("CapitalFinderGame IOException" + e.getMessage());
		// }

		// create cities
		CitiesFile cf = new CitiesFile();
		cf.insertSomeCities();
		cities = cf.getAllCities();

		usersAttempts = new ArrayList<String>();
	}

	public synchronized void play() {
		DurationGameRunnable gameRunnable = new DurationGameRunnable();
		Thread thread = new Thread(gameRunnable);
		thread.setPriority(Thread.MAX_PRIORITY);
		thread.start();

		System.out.println("Enter a capital name:");
		while (allowedAttempts > 0 && won != true) {
			allowedAttempts--;
			try {
				usersAttempts.add(readUserEntry());
			} catch (InputMismatchException ex) {
				usersAttempts.add("");
				System.out.println("Invalid Entry");
			}
			for (String city : cities) {
				for (String usersAttempt : usersAttempts) {
					if (usersAttempt.equals(city)) {
						won = true;
						displayFinalMessage();
						break;
					}
				}

			}
			if (allowedAttempts > 0)
				System.out.println("Please enter again");
		}

		synchronized (gameRunnable) {
			gameRunnable.notify();
		}

		displayFinalMessage();
	}

	public void displayFinalMessage() {

		super.displayFinalMessage();

		for (String usersAttempt : usersAttempts) {
			System.out.println("The attempt is: " + usersAttempt);
		}
		usersAttempts.clear();
		System.out.println("**********Game Over**********");
	}

	public String readUserEntry() {
		Scanner scan = new Scanner(System.in);
		String tempString = scan.next();
		return tempString;
	}

	@Override
	public void addUserAttempt(Object userEntry) throws UserAttemptException {
		if (allowedAttempts > 0) {
			for (String city : cities) {
				if (city.equals((String) userEntry)) {
					won = true;
				} else {
					won = false;
				}
			}
			allowedAttempts--;
		} else
			throw new UserAttemptException("capital finder game failed...");
	}
}
